<template>
  <div class="app-container">
    <el-form label-width="80px" size="mini">
      <el-row>
      <el-col :span="6">
        <el-form-item label="用工单号" prop="taskNumber">
          <el-input v-model="this.taskNumber" :disabled="true" maxlength="30"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="用工日期" prop="needTime">
          <el-input v-model="this.needTime" :disabled="true" maxlength="30"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="班次" prop="timeShiftName">
          <template v-for="item in operationNeedList">
            <el-input v-model="item.tsName" :disabled="true" maxlength="30"/>

          </template>

        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="男女比例" prop="manNumberScale">
          <el-input v-model="this.manAndWoman" :disabled="true" maxlength="30"/>
        </el-form-item>
      </el-col>

      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="预测零工" prop="">
            <el-input  v-model="this.personnelOddNumber" maxlength="30" :disabled="true" >
              <i slot="append" style="margin-right: 10px">人</i>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="渠道分配" prop="">
            <el-input  v-model="this.total" maxlength="30" :disabled="true" >
              <i slot="append" style="margin-right: 10px">人</i>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="人员差额" prop="">
            <el-input  v-model="this.differenceNum" maxlength="30" :disabled="true" >
              <i slot="append" style="margin-right: 10px">人</i>
            </el-input>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row>
        <el-col :span="18">
          <template v-for="item in operationNeedList">

            <StaffTable @countStaff="handleCountStaff" :tableData="item.timeShiftValue" :disabled="false"></StaffTable>
          </template>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="updateStaffAssignments">提 交</el-button>
    </div>
  </div>
</template>

<script>
  import { viewStaffAssignments , updateStaffAssignments } from "@/api/job/staffAssignments/staffAssignments";
  import { StaffTable } from "../components"
  export default {
    components:{ StaffTable },
    name: "attendanceGroup",
    created() {
      this.viewStaffAssignments();
    },
    data() {
      return {
        // 表格数据
        operationNeedList: [],
        firstOperationNeedList: [],
        numberMap:undefined,
        manNumberScale:undefined,
        womanNumberScale:undefined,
        // 是否显示弹出层（数据权限）
        openDataScope: false,
        menuExpand: false,
        menuNodeAll: false,
        deptExpand: true,
        deptNodeAll: false,
        staffAssignmentsId: this.$route.query.staffAssignmentsId,
        taskNumber:undefined,
        needTime:undefined,
        remark:undefined,
        manAndWoman:'',
        total:0,
        differenceNum:0,
        personnelOddNumber:0,
        defaultProps: {
          children: "children",
          label: "label",
        },
      };
    },

    methods: {
      //计算 渠道分配
      handleCountStaff(){
        // console.log('---',this.operationNeedList);
        let total = 0;
        this.operationNeedList.forEach(function(item) {
          let totalNum = 0;
          item.timeShiftValue.forEach(subItem=>{
            totalNum += (subItem.manNumber*1 + subItem.womanNumber*1);
          });
          item.totalNum = totalNum;
          total=total+ totalNum;
        });
        // console.log(total);
        this.total = total;

        const differenceNum =total- this.personnelOddNumber;
        if(differenceNum >0){
          this.differenceNum ="增加"+differenceNum+"人";
        }else if(differenceNum< 0){
          this.differenceNum ="减少"+Math.abs(differenceNum)+"人";
        }else{
          this.differenceNum = differenceNum;
        }
      },
      viewStaffAssignments(){
        let total =0;
        let differenceNum =0;
        viewStaffAssignments(this.staffAssignmentsId).then(es=>{

          this.operationNeedList = es.data.timeShiftData;
          this.operationNeedList.forEach(function(item) {
            total=total+item.totalNum;
          });
            this.firstOperationNeedList = JSON.parse(JSON.stringify(this.operationNeedList)),
            this.taskNumber = es.data.taskNumber,
            this.needTime = es.data.needTime,
            this.personnelOddNumber=es.data.personnelOddNumber,
            this.total=total;
          differenceNum =total-es.data.personnelOddNumber;
          if(differenceNum >0){
            this.differenceNum ="增加"+differenceNum+"人";
          }else if(differenceNum< 0){
            this.differenceNum ="减少"+Math.abs(differenceNum)+"人";
          }else{
            this.differenceNum = differenceNum;
          }

          this.numberMap = es.data.numberMap;
          this.manAndWoman ="男"+es.data.manNumberScale+"%  "+ "女"+es.data.womanNumberScale+"%";
        })
      },
      updateStaffAssignments(){
        let m = JSON.parse(JSON.stringify(this.numberMap));
        this.operationNeedList.forEach(function(item, index) {
          let keyString = item.tsName;
          let valueString = m[keyString];
          let vMNumber = valueString["man"];
          let vWNumber = valueString["woman"];
          let mNumber = 0;
          let wNumber = 0;
          item.timeShiftValue.forEach(function(v,index) {
            if ('' == String(v.manNumber)){
              alert("数据有空值");
              throw "";
            }
            if ('' == String(v.womanNumber)){
              alert("数据有空值");
              throw "";
            }
            if (Number(v.manNumber) < 0){
              alert("数据有负数");
              throw "";
            }
            if (Number(v.womanNumber) < 0){
              alert("数据有负数");
              throw "";
            }
            if (String(v.manNumber).indexOf('.') != -1) {
              alert("数据有小数");
              throw "";
            }
            if (String(v.womanNumber).indexOf('.') != -1) {
              alert("数据有小数");
              throw "";
            }
            mNumber += Number(v.manNumber);
            wNumber += Number(v.womanNumber);
          })
          // if (vMNumber != mNumber) {
          //   alert(keyString+"男性人数分配有差异，请重新分配");
          //   throw "";
          // }
          // if (vWNumber != wNumber) {
          //   alert(keyString+"女性人数分配有差异，请重新分配");
          //   throw "";
          // }
        })
        const data = {
          operationNeedList: this.operationNeedList,
          taskNumber: this.taskNumber,
          needTime: this.needTime,
          staffAssignmentsId : this.staffAssignmentsId,
          manNumberScale : this.manNumberScale,
          womanNumberScale : this.womanNumberScale,
          firstOperationNeedList : this.firstOperationNeedList
        }
        // console.log(data)
        updateStaffAssignments(data).then(es => {
          this.msgSuccess("提交成功");
          this.open = false;
        })
      },
    }
  };
</script>
